{% extends "base.html" %}
{% block title %}{{vote.title}}的投票页面{% endblock %}
{% block content %}
<div class="jumbotron">
    <h2>这里是{{vote.title}}的活动页面,请大家踊跃投票</h2>
</div>
<div class="row">
    {% if items %}
      {% for item in items %}
      <div class="col-sm-6 col-md-3">
        <div class="thumbnail">
          <img src="{{item.pic_url}}" style="width:242px;height:200px">
          <div class="caption">
            <h3>{{item.item_id}}:{{item.title}}</h3>
              <p>票数:<span id="{{item.id}}_count">{{item.vote_count}}</span></p>
            <p><div id="{{item.id}}" onclick="add_count(this)" class="btn btn-primary" role="button">投一票</div>
              <a href="{{item.url}}" target="_blank" class="btn btn-default" role="button">详情</a></p>
          </div>
        </div>
      </div>
    {% endfor %}
  {% endif %}

</div>
{% endblock %}


{% block script %}
<script type="text/javascript">


function add_count(obj){
    var post_url = "/add_count/" + String(obj.id) + "/"
    var count_id = String(obj.id) + "_count"
     $.ajax({
        url: post_url,
        useDefaultXhrHeader: false, //important, otherwise its not working
        type: "POST",
        success: function(response, opts) {
            console.log(response)
            //data = JSON.parse(response)
            console.log(count_id)
            document.getElementById(count_id).textContent = response.count
        }
     })
}
</script>

{% endblock %}